package org.nf.servlet;

import com.google.gson.Gson;
import org.nf.dbutil.GsonUtil;
import org.nf.entity.User;
import org.nf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author lzp
 * @Date 2024-04-25
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //Servlet -> Service -> Dao
        UserService userService = new UserService();
        List<User> list = userService.findAllUser();

        String json = GsonUtil.toJson(list);

        PrintWriter out = resp.getWriter();
        out.println(json);
        out.flush();
        out.close();
    }
}
